home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / gfx / show / vmpeg.lha / src / mpegaudio / common.c next >
Encoding:
C/C++ Source or Header  |  1999-07-06  |  23.2 KB  |  691 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. common.c
  4. **********************************************************************/
  5. /**********************************************************************
  6.  * MPEG/audio coding/decoding software, work in progress              *
  7.  *   NOT for public distribution until verified and approved by the   *
  8.  *   MPEG/audio committee.  For further information, please contact   *
  9.  *   Davis Pan, 508-493-2241, e-mail: pan@3d.enet.dec.com             *
  10.  *                                                                    *
  11.  * VERSION 4.0                                                        *
  12.  *   changes made since last update:                                  *
  13.  *   date   programmers         comment                               *
  14.  * 2/25/91  Doulas Wong,        start of version 1.0 records          *
  15.  *          Davis Pan                                                 *
  16.  * 5/10/91  W. Joseph Carter    Created this file for all common      *
  17.  *                              functions and global variables.       *
  18.  *                              Ported to Macintosh and Unix.         *
  19.  *                              Added Jean-Georges Fritsch's          *
  20.  *                              "bitstream.c" package.                *
  21.  *                              Added routines to handle AIFF PCM     *
  22.  *                              sound files.                          *
  23.  *                              Added "mem_alloc()" and "mem_free()"  *
  24.  *                              routines for memory allocation        *
  25.  *                              portability.                          *
  26.  *                              Added routines to convert between     *
  27.  *                              Apple SANE extended floating point    *
  28.  *                              format and IEEE double precision      *
  29.  *                              floating point format.  For AIFF.     *
  30.  * 02jul91 dpwe (Aware Inc)     Moved allocation table input here;    *
  31.  *                              Tables read from subdir TABLES_PATH.  *
  32.  *                              Added some debug printout fns (Write*)*
  33.  * 7/10/91 Earle Jennings       replacement of the one float by FLOAT *
  34.  *                              port to MsDos from MacIntosh version  *
  35.  * 8/ 5/91 Jean-Georges Fritsch fixed bug in open_bit_stream_r()      *
  36.  *10/ 1/91 S.I. Sudharsanan,    Ported to IBM AIX platform.           *
  37.  *         Don H. Lee,                                                *
  38.  *         Peter W. Farrett                                           *
  39.  *10/3/91  Don H. Lee           implemented CRC-16 error protection   *
  40.  *                              newly introduced functions are        *
  41.  *                              I_CRC_calc, II_CRC_calc and           *
  42.  *                              update_CRC. Additions and revisions   *
  43.  *                              are marked with dhl for clarity       *
  44.  *10/18/91 Jean-Georges Fritsch fixed bug in update_CRC(),            *
  45.  *                              II_CRC_calc() and I_CRC_calc()        *
  46.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  47.  *                              important fixes involved changing     *
  48.  *                              16-bit ints to long or unsigned in    *
  49.  *                              bit alloc routines for quant of 65535 *
  50.  *                              and passing proper function args.     *
  51.  *                              Removed "Other Joint Stereo" option   *
  52.  *                              and made bitrate be total channel     *
  53.  *                              bitrate, irrespective of the mode.    *
  54.  *                              Fixed many small bugs & reorganized.  *
  55.  * 3/20/92 Jean-Georges Fritsch  fixed bug in start-of-frame search   *
  56.  * 6/15/92 Juan Pineda          added refill_buffer(bs) "n"           *
  57.  *                              initialization                        *
  58.  * 7/08/92 Susanne Ritscher     MS-DOS, MSC6.0 port fixes             *
  59.  * 7/27/92 Mike Li               (re-)Port to MS-DOS                  *
  60.  * 8/19/92 Soren H. Nielsen     Fixed bug in I_CRC_calc and in        *
  61.  *                              II_CRC_calc.  Added function: new_ext *
  62.  *                              for better MS-DOS compatability       *
  63.  * 3/10/93 Kevin Peterson       changed aiff_read_headers to handle   *
  64.  *                              chunks in any order.  now returns     *
  65.  *                              position of sound data in file.       *
  66.  * 3/31/93 Jens Spille          changed IFF_* string compares to use  *
  67.  *                              strcmp()                              *
  68.  * 5/30/93 Masahiro Iwadare ?? the previous modification does not *
  69.  *          work. recovered to the original. ?? *
  70.  * 8/27/93 Seymour Shlien,      Fixes in Unix and MSDOS ports,        *
  71.  *         Daniel Lauzon, and                                         *
  72.  *         Bill Truerniet                                             *
  73.  **********************************************************************/
  74.  
  75. /***********************************************************************
  76. *
  77. *  Global Include Files
  78. *
  79. ***********************************************************************/
  80.  
  81. #include        "common.h"
  82.  
  83. #ifdef  MACINTOSH
  84.  
  85. #include        <SANE.h>
  86. #include        <pascal.h>
  87.  
  88. #endif
  89.  
  90. #include <ctype.h>
  91. #include "audio.h"
  92.  
  93. /***********************************************************************
  94. *
  95. *  Global Variable Definitions
  96. *
  97. ***********************************************************************/
  98.  
  99. char *mode_names[4] = { "stereo", "j-stereo", "dual-ch", "single-ch" };
  100. char *layer_names[3] = { "I", "II", "III" };
  101.  
  102. double  s_freq[4] = {44.1, 48, 32, 0};
  103.  
  104. int     bitrate[3][15] = {
  105.           {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448},
  106.           {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384},
  107.           {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320}
  108.         };
  109.  
  110. double FAR multiple[64] = {
  111. 2.00000000000000, 1.58740105196820, 1.25992104989487,
  112. 1.00000000000000, 0.79370052598410, 0.62996052494744, 0.50000000000000,
  113. 0.39685026299205, 0.31498026247372, 0.25000000000000, 0.19842513149602,
  114. 0.15749013123686, 0.12500000000000, 0.09921256574801, 0.07874506561843,
  115. 0.06250000000000, 0.04960628287401, 0.03937253280921, 0.03125000000000,
  116. 0.02480314143700, 0.01968626640461, 0.01562500000000, 0.01240157071850,
  117. 0.00984313320230, 0.00781250000000, 0.00620078535925, 0.00492156660115,
  118. 0.00390625000000, 0.00310039267963, 0.00246078330058, 0.00195312500000,
  119. 0.00155019633981, 0.00123039165029, 0.00097656250000, 0.00077509816991,
  120. 0.00061519582514, 0.00048828125000, 0.00038754908495, 0.00030759791257,
  121. 0.00024414062500, 0.00019377454248, 0.00015379895629, 0.00012207031250,
  122. 0.00009688727124, 0.00007689947814, 0.00006103515625, 0.00004844363562,
  123. 0.00003844973907, 0.00003051757813, 0.00002422181781, 0.00001922486954,
  124. 0.00001525878906, 0.00001211090890, 0.00000961243477, 0.00000762939453,
  125. 0.00000605545445, 0.00000480621738, 0.00000381469727, 0.00000302772723,
  126. 0.00000240310869, 0.00000190734863, 0.00000151386361, 0.00000120155435,
  127. 1E-20
  128. };
  129.  
  130. struct alloc_entry
  131. {
  132. unsigned int    steps;
  133. unsigned int    bits;
  134. unsigned int    group;
  135. unsigned int    quant;
  136. };
  137.  
  138. #include "tables/alloc_0.h"
  139. #include "tables/alloc_1.h"
  140. #include "tables/alloc_2.h"
  141. #include "tables/alloc_3.h"
  142.  
  143. /***********************************************************************
  144. *
  145. *  Global Function Definitions
  146. *
  147. ***********************************************************************/
  148.  
  149. /* The system uses a variety of data files.  By opening them via this
  150.    function, we can accommodate various locations. */
  151.  
  152. /***********************************************************************
  153. /*
  154. /* Read one of the data files ("alloc_*") specifying the bit allocation/
  155. /* quatization parameters for each subband in layer II encoding
  156. /*
  157. /**********************************************************************/
  158.  
  159. /***********************************************************************
  160. /*
  161. /* Using the decoded info the appropriate possible quantization per
  162. /* subband table is loaded
  163. /*
  164. /**********************************************************************/
  165.  
  166. int pick_table(fr_ps)   /* choose table, load if necess, return # sb's */
  167. frame_params *fr_ps;
  168. {
  169.     int table, lay, ws, bsp, br_per_ch, sfrq;
  170.     int sblim = fr_ps->sblimit;     /* return current value if no load */
  171.  
  172.     lay = fr_ps->header->lay - 1;
  173.     bsp = fr_ps->header->bitrate_index;
  174.     br_per_ch = bitrate[lay][bsp] / fr_ps->stereo;
  175.     ws = fr_ps->header->sampling_frequency;
  176.     sfrq = s_freq[ws];
  177.     /* decision rules refer to per-channel bitrates (kbits/sec/chan) */
  178.     if ((sfrq == 48 && br_per_ch >= 56) ||
  179.         (br_per_ch >= 56 && br_per_ch <= 80)) table = 0;
  180.     else if (sfrq != 48 && br_per_ch >= 96) table = 1;
  181.     else if (sfrq != 32 && br_per_ch <= 48) table = 2;
  182.     else table = 3;
  183.     if (fr_ps->tab_num != table) {
  184.  
  185.     switch((fr_ps->tab_num = table))
  186.     {
  187.       default:
  188.       case 0:
  189.         printf("using bit-allocation table 0.\n");
  190.         fr_ps->alloc = (al_table *)&t_alloc_0;
  191.         sblim = 27;
  192.       break;
  193.       case 1:
  194.         printf("using bit-allocation table 1.\n");
  195.         fr_ps->alloc = (al_table *)&t_alloc_1;
  196.         sblim = 30;
  197.       break;
  198.       case 2:
  199.         printf("using bit-allocation table 2.\n");
  200.         fr_ps->alloc = (al_table *)&t_alloc_2;
  201.         sblim = 8;
  202.       break;
  203.       case 3:
  204.         printf("using bit-allocation table 3.\n");
  205.         fr_ps->alloc = (al_table *)&t_alloc_3;
  206.         sblim = 12;
  207.       break;
  208.     }
  209.   }
  210.   return sblim;
  211. }
  212.  
  213. int js_bound(lay, m_ext)
  214. int lay, m_ext;
  215. {
  216. static int jsb_table[3][4] =  { { 4, 8, 12, 16 }, { 4, 8, 12, 16},
  217.                                 { 0, 4, 8, 16} };  /* lay+m_e -> jsbound */
  218.  
  219.     if(lay<1 || lay >3 || m_ext<0 || m_ext>3) {
  220.         fprintf(stderr, "js_bound bad layer/modext (%d/%d)\n", lay, m_ext);
  221.         exit(1);
  222.     }
  223.     return(jsb_table[lay-1][m_ext]);
  224. }
  225.  
  226. void hdr_to_frps(fr_ps) /* interpret data in hdr str to fields in fr_ps */
  227. frame_params *fr_ps;
  228. {
  229. layer *hdr = fr_ps->header;     /* (or pass in as arg?) */
  230.  
  231.     fr_ps->actual_mode = hdr->mode;
  232.     fr_ps->stereo = (hdr->mode == MPG_MD_MONO) ? 1 : 2;
  233.     if (hdr->lay == 2)          fr_ps->sblimit = pick_table(fr_ps);
  234.     else                        fr_ps->sblimit = SBLIMIT;
  235.     if(hdr->mode == MPG_MD_JOINT_STEREO)
  236.         fr_ps->jsbound = js_bound(hdr->lay, hdr->mode_ext);
  237.     else
  238.         fr_ps->jsbound = fr_ps->sblimit;
  239.     /* alloc, tab_num set in pick_table */
  240. }
  241.  
  242. /*******************************************************************************
  243. *
  244. *  Allocate number of bytes of memory equal to "block".
  245. *
  246. *******************************************************************************/
  247.  
  248. void  FAR *mem_alloc(block, item)
  249. unsigned long   block;
  250. char            *item;
  251. {
  252.  
  253.     void    *ptr;
  254.  
  255. #ifdef  MACINTOSH
  256.     ptr = NewPtr(block);
  257. #endif
  258.  
  259. #ifdef MSC60
  260.     /*ptr = (void FAR *) _fmalloc((unsigned int)block);*/ /* far memory, 92-07-08 sr */
  261.     ptr = (void FAR *) malloc((unsigned int)block); /* far memory, 93-08-24 ss */
  262. #endif
  263.  
  264. #if ! defined (MACINTOSH) && ! defined (MSC60)
  265.     ptr = (void FAR *) malloc(block);
  266. #endif
  267.  
  268.     if (ptr != NULL){
  269. #ifdef  MSC60
  270.         _fmemset(ptr, 0, (unsigned int)block); /* far memory, 92-07-08 sr */
  271. #else
  272.         memset(ptr, 0, block);
  273. #endif
  274.     }
  275.     else{
  276.         printf("Unable to allocate %s\n", item);
  277.         exit(0);
  278.     }
  279.     return(ptr);
  280. }
  281.  
  282.  
  283. /****************************************************************************
  284. *
  285. *  Free memory pointed to by "*ptr_addr".
  286. *
  287. *****************************************************************************/
  288.  
  289. void    mem_free(ptr_addr)
  290. void    **ptr_addr;
  291. {
  292.  
  293.     if (*ptr_addr != NULL){
  294. #ifdef  MACINTOSH
  295.         DisposPtr(*ptr_addr);
  296. #else
  297.         free(*ptr_addr);
  298. #endif
  299.         *ptr_addr = NULL;
  300.     }
  301.  
  302. }
  303.  
  304. /*******************************************************************************
  305. *
  306. *  Check block of memory all equal to a single byte, else return FALSE
  307. *
  308. *******************************************************************************/
  309.  
  310. int memcheck(array, test, num)
  311. char *array;
  312. int test;       /* but only tested as a char (bottom 8 bits) */
  313. int num;
  314. {
  315.  int i=0;
  316.  
  317.    while (array[i] == test && i<num) i++;
  318.    if (i==num) return TRUE;
  319.    else return FALSE;
  320. }
  321.  
  322. /****************************************************************************
  323. *
  324. *  Routines to convert between the Apple SANE extended floating point format
  325. *  and the IEEE double precision floating point format.  These routines are
  326. *  called from within the Audio Interchange File Format (AIFF) routines.
  327. *
  328. *****************************************************************************/
  329.  
  330. /*
  331. *** Apple's 80-bit SANE extended has the following format:
  332.  
  333.  1       15      1            63
  334. +-+-------------+-+-----------------------------+
  335. |s|       e     |i|            f                |
  336. +-+-------------+-+-----------------------------+
  337.   msb        lsb   msb                       lsb
  338.  
  339. The value v of the number is determined by these fields as follows:
  340. If 0 <= e < 32767,              then v = (-1)^s * 2^(e-16383) * (i.f).
  341. If e == 32767 and f == 0,       then v = (-1)^s * (infinity), regardless of i.
  342. If e == 32767 and f != 0,       then v is a NaN, regardless of i.
  343.  
  344. *** IEEE Draft Standard 754 Double Precision has the following format:
  345.  
  346. MSB
  347. +-+---------+-----------------------------+
  348. |1| 11 Bits |           52 Bits           |
  349. +-+---------+-----------------------------+
  350.  ^     ^                ^
  351.  |     |                |
  352.  Sign  Exponent         Mantissa
  353. */
  354.  
  355. /****  for debugging 
  356. showchar(str)
  357. char str[4];
  358. {
  359. int i;
  360. for (i=0;i<4;i++) printf("%c",str[i]);
  361. printf("\n");
  362. }
  363. ****/
  364.  
  365. /*****************************************************************************
  366. *
  367. *  bit_stream.c package
  368. *  Author:  Jean-Georges Fritsch, C-Cube Microsystems
  369. *
  370. *****************************************************************************/
  371.  
  372. /********************************************************************
  373.   This package provides functions to write (exclusive or read)
  374.   information from (exclusive or to) the bit stream.
  375.  
  376.   If the bit stream is opened in read mode only the get functions are
  377.   available. If the bit stream is opened in write mode only the put
  378.   functions are available.
  379. ********************************************************************/
  380.  
  381. /*open_bit_stream_r(); open the device to read the bit stream from it     */
  382. /*close_bit_stream();  close the device containing the bit stream         */
  383. /*alloc_buffer();      open and initialize the buffer;                    */
  384. /*desalloc_buffer();   empty and close the buffer                         */
  385. /*unsigned int get1bit();  read 1 bit from the bit stream                 */
  386. /*unsigned long getbits(); read N bits from the bit stream                */
  387. /*unsigned long look_ahead(); grep the next N bits in the bit stream without*/
  388. /*                            changing the buffer pointer                   */
  389. /*unsigned long sstell(); return the current bit stream length (in bits)    */
  390. /*int seek_sync(); return 1 if a sync word was found in the bit stream      */
  391. /*                 otherwise returns 0                                      */
  392.  
  393. #ifndef __PPC__
  394. /* refill the buffer from the input device when the buffer becomes empty    */
  395. void refill_buffer(bs)
  396. Bit_stream_struc *bs;   /* bit stream structure */
  397. {
  398.    register int i=bs->buf_size-2-bs->buf_byte_idx;
  399.    register unsigned long n=1;
  400.    register int index=0;
  401.    char val[2];
  402.  
  403.    while ((i>=0) && (!bs->eob))
  404.   bs->buf[i--] = audio_pollbuffer();
  405. }
  406. #endif
  407.  
  408. static char *he = "0123456789ABCDEF";
  409.  
  410.  
  411. /* open the device to read the bit stream from it */
  412. void open_bit_stream_r(bs, bs_filenam, size)
  413. Bit_stream_struc *bs;   /* bit stream structure */
  414. char *bs_filenam;       /* name of the bit stream file */
  415. int size;               /* size of the buffer */
  416. {
  417.    bs->format = BINARY;
  418.    alloc_buffer(bs, size);
  419.    bs->buf_byte_idx=0;
  420.    bs->buf_bit_idx=0;
  421.    bs->totbit=0;
  422.    bs->mode = READ_MODE;
  423.    bs->eob = FALSE;
  424.    bs->eobs = FALSE;
  425. }
  426.  
  427. /*close the device containing the bit stream after a read process*/
  428. void close_bit_stream_r(bs)
  429. Bit_stream_struc *bs;   /* bit stream structure */
  430. {
  431.    desalloc_buffer(bs);
  432. }
  433.  
  434. /*close the device containing the bit stream after a write process*/
  435. void close_bit_stream_w(bs)
  436. Bit_stream_struc *bs;   /* bit stream structure */
  437. {
  438.    desalloc_buffer(bs);
  439. }
  440.  
  441. /*open and initialize the buffer; */
  442. void alloc_buffer(bs, size)
  443. Bit_stream_struc *bs;   /* bit stream structure */
  444. int size;
  445. {
  446.    bs->buf = (unsigned char FAR *) mem_alloc(size*sizeof(unsigned
  447.               char), "buffer");
  448.    bs->buf_size = size;
  449. }
  450.  
  451. /*empty and close the buffer */
  452. void desalloc_buffer(bs)
  453. Bit_stream_struc *bs;   /* bit stream structure */
  454. {
  455.    free(bs->buf);
  456. }
  457.  
  458. int putmask[9]={0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff};
  459. int clearmask[9]={0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x0};
  460.  
  461. int mask[8]={0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
  462.  
  463. #ifndef __PPC__
  464. /*read 1 bit from the bit stream */
  465. unsigned int get1bit(bs)
  466. Bit_stream_struc *bs;   /* bit stream structure */
  467. {
  468.    unsigned int bit;
  469.    register int i;
  470.  
  471.    bs->totbit++;
  472.  
  473.    if (!bs->buf_bit_idx) {
  474.         bs->buf_bit_idx = 8;
  475.         bs->buf_byte_idx--;
  476.         if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {
  477.              if (bs->eob)
  478.                 bs->eobs = TRUE;
  479.              else {
  480.                 for (i=bs->buf_byte_idx; i>=0;i--)
  481.                   bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i];
  482.                 refill_buffer(bs);
  483.                 bs->buf_byte_idx = bs->buf_size-1;
  484.              }
  485.         }
  486.    }
  487.    bit = bs->buf[bs->buf_byte_idx]&mask[bs->buf_bit_idx-1];
  488.    bit = bit >> (bs->buf_bit_idx-1);
  489.    bs->buf_bit_idx--;
  490.    return(bit);
  491. }
  492. #endif
  493.  
  494. /*look ahead for the next N bits from the bit stream */
  495. unsigned long look_ahead(bs, N)
  496. Bit_stream_struc *bs;   /* bit stream structure */
  497. int N;                  /* number of bits to read from the bit stream */
  498. {
  499.  unsigned long val=0;
  500.  register int j = N;
  501.  register int k, tmp;
  502.  register int bit_idx = bs->buf_bit_idx;
  503.  register int byte_idx = bs->buf_byte_idx;
  504.  
  505.  if (N > MAX_LENGTH)
  506.     printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);
  507.  
  508.  while (j > 0) {
  509.     if (!bit_idx) {
  510.         bit_idx = 8;
  511.         byte_idx--;
  512.     }
  513.     k = MIN (j, bit_idx);
  514.     tmp = bs->buf[byte_idx]&putmask[bit_idx];
  515.     tmp = tmp >> (bit_idx-k);
  516.     val |= tmp << (j-k);
  517.     bit_idx -= k;
  518.     j -= k;
  519.  }
  520.  return(val);
  521. }
  522.  
  523. #ifndef __PPC__
  524. /*read N bit from the bit stream */
  525. unsigned long getbits(bs, N)
  526. Bit_stream_struc *bs;   /* bit stream structure */
  527. int N;                  /* number of bits to read from the bit stream */
  528. {
  529.  unsigned long val=0;
  530.  register int i;
  531.  register int j = N;
  532.  register int k, tmp;
  533.  
  534.  if (N > MAX_LENGTH)
  535.     printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);
  536.  
  537.  bs->totbit += N;
  538.  while (j > 0) {
  539.    if (!bs->buf_bit_idx) {
  540.         bs->buf_bit_idx = 8;
  541.         bs->buf_byte_idx--;
  542.         if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {
  543.              if (bs->eob)
  544.                 bs->eobs = TRUE;
  545.              else {
  546.                 for (i=bs->buf_byte_idx; i>=0;i--)
  547.                    bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i];
  548.                 refill_buffer(bs);
  549.                 bs->buf_byte_idx = bs->buf_size-1;
  550.              }
  551.         }
  552.    }
  553.    k = MIN (j, bs->buf_bit_idx);
  554.    tmp = bs->buf[bs->buf_byte_idx]&putmask[bs->buf_bit_idx];
  555.    tmp = tmp >> (bs->buf_bit_idx-k);
  556.    val |= tmp << (j-k);
  557.    bs->buf_bit_idx -= k;
  558.    j -= k;
  559.  }
  560.  return(val);
  561. }
  562. #endif
  563.  
  564. #if 0
  565. /*return the current bit stream length (in bits)*/
  566. unsigned long sstell(bs)
  567. Bit_stream_struc *bs;   /* bit stream structure */
  568. {
  569.   return(bs->totbit);
  570. }
  571. #endif
  572.  
  573. /*this function seeks for a byte aligned sync word in the bit stream and
  574.   places the bit stream pointer right after the sync.
  575.   This function returns 1 if the sync was found otherwise it returns 0  */
  576. int seek_sync(bs, sync, N)
  577. Bit_stream_struc *bs;   /* bit stream structure */
  578. long sync;      /* sync word maximum 32 bits */
  579. int N;          /* sync word length */
  580. {
  581.  double pow();
  582.  unsigned long aligning, stell();
  583.  unsigned long val;
  584.  long maxi = (int)pow(2.0, (FLOAT)N) - 1;
  585.  
  586.  aligning = sstell(bs)%ALIGNING;
  587.  if (aligning)
  588.     getbits(bs, (int)(ALIGNING-aligning));
  589.  
  590.   val = getbits(bs, N);
  591.   while (((val&maxi) != sync) && (audioBufferSize>0)) {
  592.         val <<= ALIGNING;
  593.         val |= getbits(bs, ALIGNING);
  594.   }
  595.  
  596.  if (!audioBufferSize) return(0);
  597.  else return(1);
  598. }
  599. /*****************************************************************************
  600. *
  601. *  End of bit_stream.c package
  602. *
  603. *****************************************************************************/
  604.  
  605. /*****************************************************************************
  606. *
  607. *  CRC error protection package
  608. *
  609. *****************************************************************************/
  610.  
  611. void I_CRC_calc(fr_ps, bit_alloc, crc)
  612. frame_params *fr_ps;
  613. unsigned int bit_alloc[2][SBLIMIT];
  614. unsigned int *crc;
  615. {
  616.         int i, k;
  617.         layer *info = fr_ps->header;
  618.         int stereo  = fr_ps->stereo;
  619.         int jsbound = fr_ps->jsbound;
  620.  
  621.         *crc = 0xffff; /* changed from '0' 92-08-11 shn */
  622.         update_CRC(info->bitrate_index, 4, crc);
  623.         update_CRC(info->sampling_frequency, 2, crc);
  624.         update_CRC(info->padding, 1, crc);
  625.         update_CRC(info->extension, 1, crc);
  626.         update_CRC(info->mode, 2, crc);
  627.         update_CRC(info->mode_ext, 2, crc);
  628.         update_CRC(info->copyright, 1, crc);
  629.         update_CRC(info->original, 1, crc);
  630.         update_CRC(info->emphasis, 2, crc);
  631.  
  632.         for (i=0;i<SBLIMIT;i++)
  633.                 for (k=0;k<((i<jsbound)?stereo:1);k++)
  634.                         update_CRC(bit_alloc[k][i], 4, crc);
  635. }
  636.  
  637. void II_CRC_calc(fr_ps, bit_alloc, scfsi, crc)
  638. frame_params *fr_ps;
  639. unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];
  640. unsigned int *crc;
  641. {
  642.         int i, k;
  643.         layer *info = fr_ps->header;
  644.         int stereo  = fr_ps->stereo;
  645.         int sblimit = fr_ps->sblimit;
  646.         int jsbound = fr_ps->jsbound;
  647.         al_table *alloc = fr_ps->alloc;
  648.  
  649.         *crc = 0xffff; /* changed from '0' 92-08-11 shn */
  650.         update_CRC(info->bitrate_index, 4, crc);
  651.         update_CRC(info->sampling_frequency, 2, crc);
  652.         update_CRC(info->padding, 1, crc);
  653.         update_CRC(info->extension, 1, crc);
  654.         update_CRC(info->mode, 2, crc);
  655.         update_CRC(info->mode_ext, 2, crc);
  656.         update_CRC(info->copyright, 1, crc);
  657.         update_CRC(info->original, 1, crc);
  658.         update_CRC(info->emphasis, 2, crc);
  659.  
  660.         for (i=0;i<sblimit;i++)
  661.                 for (k=0;k<((i<jsbound)?stereo:1);k++)
  662.                         update_CRC(bit_alloc[k][i], (*alloc)[i][0].bits, crc);
  663.  
  664.         for (i=0;i<sblimit;i++)
  665.                 for (k=0;k<stereo;k++)
  666.                         if (bit_alloc[k][i])
  667.                                 update_CRC(scfsi[k][i], 2, crc);
  668. }
  669.  
  670. void update_CRC(data, length, crc)
  671. unsigned int data, length, *crc;
  672. {
  673.         unsigned int  masking, carry;
  674.  
  675.         masking = 1 << length;
  676.  
  677.         while((masking >>= 1)){
  678.                 carry = *crc & 0x8000;
  679.                 *crc <<= 1;
  680.                 if (!carry ^ !(data & masking))
  681.                         *crc ^= CRC16_POLYNOMIAL;
  682.         }
  683.         *crc &= 0xffff;
  684. }
  685.  
  686. /*****************************************************************************
  687. *
  688. *  End of CRC error protection package
  689. *
  690. *****************************************************************************/
  691.